package initialize

import (
	"context"
	"os"
	"server/global"

	"github.com/redis/go-redis/v9"
	"go.uber.org/zap"
)

// InitRedis 初始化Redis客户端
func InitRedis() redis.Client {
	redisCfg := global.Config.Redis

	client := redis.NewClient(&redis.Options{
		Addr:     redisCfg.Address,
		Password: redisCfg.Password,
		DB:       redisCfg.DB,
	})
	ctx := context.Background()
	// Ping Redis 服务器以检查连接是否正常
	_, err := client.Ping(ctx).Result()
	if err != nil {
		global.Log.Error("Failed to connect to Redis:", zap.Error(err))
		os.Exit(1)
	}
	global.Log.Info("success to connect to Redis")
	return *client
}
